package com.jzt.gateway.collect;

import com.jzt.gateway.GlobalConstant;
import com.jzt.gateway.dto.request.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.springframework.web.server.ServerWebExchange;

/**
 * 作者：lizw <br/>
 * 创建时间：2020/06/18 14:37 <br/>
 */
@Slf4j
public abstract class AbstractCollectLoginData implements ICollectLoginData {
    /**
     * 登录模式
     */
    public static final String LoginMode = "mode";
    /**
     * 用户名密码登录
     */
    public static final String LoginMode_LoginName = "1";
    /**
     * 手机号验证码登录
     */
    public static final String LoginMode_TelephoneValidateCode = "2";
    /**
     * APP扫码登录
     */
    public static final String LoginMode_AppScanCode = "3";
    /**
     * 微信小程序登录
     */
    public static final String LoginMode_WechatSmallProgram = "4";

    @Override
    public int order() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean support(ServerWebExchange exchange) {
        return true;
    }

    @Override
    public AbstractLogin getLoginInfo(ServerWebExchange exchange) {
        String body = exchange.getAttribute(GlobalConstant.CACHED_REQUEST_BODY_STR_ATTR);
        JSONObject jsonBody = new JSONObject(body);
        String loginMode = jsonBody.has(LoginMode) ? jsonBody.getString(LoginMode) : LoginMode_LoginName;
        AbstractLogin loginInfo = null;
        switch (StringUtils.trimToEmpty(loginMode)) {
            // 1:用户名密码登录
            case LoginMode_LoginName:
                loginInfo = getLoginNamePassword(exchange, jsonBody);
                break;
            // 手机号验证码登录
            case LoginMode_TelephoneValidateCode:
                loginInfo = getTelephoneValidateCode(exchange, jsonBody);
                break;
            // APP扫码登录
            case LoginMode_AppScanCode:
                loginInfo = getAppScanCode(exchange, jsonBody);
                break;
            // 微信小程序登录
            case LoginMode_WechatSmallProgram:
                loginInfo = getWechatSmallProgram(exchange, jsonBody);
                break;
            default:
        }
        return loginInfo;
    }

    /**
     * 获取用户名密码登录信息
     */
    protected LoginNamePassword getLoginNamePassword(ServerWebExchange exchange, JSONObject jsonBody) {
        final String nameParamName = "name";
        final String passwordParamName = "password";
        String name = jsonBody.has(nameParamName) ? jsonBody.getString(nameParamName) : null;
        String password = jsonBody.has(passwordParamName) ? jsonBody.getString(passwordParamName) : null;
        LoginNamePassword loginNamePassword = new LoginNamePassword();
        loginNamePassword.setName(name);
        loginNamePassword.setPassword(password);
        return loginNamePassword;
    }

    /**
     * 获取手机号验证码登录信息
     */
    protected TelephoneValidateCode getTelephoneValidateCode(ServerWebExchange exchange, JSONObject jsonBody) {
        final String telephoneParamName = "telephone";
        final String validateCodeParamName = "validateCode";
        String telephone = jsonBody.has(telephoneParamName) ? jsonBody.getString(telephoneParamName) : null;
        String validateCode = jsonBody.has(validateCodeParamName) ? jsonBody.getString(validateCodeParamName) : null;
        TelephoneValidateCode telephoneValidateCode = new TelephoneValidateCode();
        telephoneValidateCode.setTelephone(telephone);
        telephoneValidateCode.setValidateCode(validateCode);
        return telephoneValidateCode;
    }

    /**
     * 获取APP扫码登录信息
     */
    protected AppScanCode getAppScanCode(ServerWebExchange exchange, JSONObject jsonBody) {
        final String scanCodeParamName = "scanCode";
        final String appTokenParamName = "appToken";
        String scanCode = jsonBody.has(scanCodeParamName) ? jsonBody.getString(scanCodeParamName) : null;
        String appToken = jsonBody.has(appTokenParamName) ? jsonBody.getString(appTokenParamName) : null;

        return null;
    }

    /**
     * 获取微信小程序登录信息
     */
    protected WechatSmallProgram getWechatSmallProgram(ServerWebExchange exchange, JSONObject jsonBody) {
        final String loginCodeParamName = "loginCode";
        String loginCode = jsonBody.has(loginCodeParamName) ? jsonBody.getString(loginCodeParamName) : null;

        return null;
    }
}
